package com.jtjrenren.android.taxi.passenger.core.app.splash;

import com.jtjrenren.android.taxi.passenger.Constants;
import com.jtjrenren.android.taxi.passenger.base.BaseApplication;
import com.jtjrenren.android.taxi.passenger.manger.api.ApiWorks;
import com.library.utils.AppUtils;
import com.library.utils.LogUtil;
import com.liulishuo.filedownloader.BaseDownloadTask;
import com.liulishuo.filedownloader.FileDownloadListener;
import com.liulishuo.filedownloader.FileDownloader;



import java.io.File;

/**
 * Created by Administrator on 2016/9/9.
 */

public class SplashInteractorImpl implements SplashInteractor {


    @Override
    public void downApp(String url, final DownListenr listenr) {
        LogUtil.log("dwon","下载app的地址是："+url);
        String appPath = Constants.APP_DEFAULT_PATH;
        File file = new File(appPath);
        if (!file.exists())
            file.mkdirs();
        appPath = appPath + File.separator + "temp.apk";
        file = new File(appPath);
        if (file.exists())
            file.delete();

        FileDownloader.getImpl().create(url).setPath(appPath)
                .setListener(new FileDownloadListener() {
                    @Override
                    protected void pending(BaseDownloadTask task, int soFarBytes, int totalBytes) {

                    }

                    @Override
                    protected void progress(BaseDownloadTask task, int soFarBytes, int totalBytes) {
                        int process = (int) ((soFarBytes / (float) totalBytes) * 100);
                        LogUtil.log("dwon", "down progress:" + process);
                    }

                    @Override
                    protected void completed(BaseDownloadTask task) {
                        LogUtil.log("dwon", "down completed:" + task.getPath());
                        listenr.finish(task.getPath());
                    }

                    @Override
                    protected void paused(BaseDownloadTask task, int soFarBytes, int totalBytes) {

                    }

                    @Override
                    protected void error(BaseDownloadTask task, Throwable e) {
                        LogUtil.log("dwon", "down error:" + e.toString());
                        listenr.error(e.toString());
                    }

                    @Override
                    protected void warn(BaseDownloadTask task) {
                        LogUtil.log("dwon", "down warn:");
                    }
                }).start();
    }

    @Override
    public void checkVersion(ApiWorks.ResponseListener listener) {
        ApiWorks.checkNewVersion_M(AppUtils.getVersionName(BaseApplication.getInstance().getApplicationContext()), listener);
    }

}
